package com.raccoonagents.tools.cascet2d.model.components;

import com.raccoonagents.tools.cascet2d.interfaces.IMouseMotionListener;
import com.raccoonagents.tools.cascet2d.interfaces.IMouseTarget;
import com.raccoonagents.tools.cascet2d.interfaces.IUpdateable;
import com.raccoonagents.tools.cascet2d.model.Vector2f;
import com.raccoonagents.tools.cascet2d.util.CalculationUtils;
import com.raccoonagents.tools.cascet2d.util.MouseManager;
import com.raccoonagents.tools.cascet2d.util.MouseManager.MouseState;
/**
 * 
 * @author Kari Kuvaja
 * an abstract button class to be extented by implementations.
 */

public abstract class AbstractButton extends AbstractStageUIComponent implements IMouseTarget, IUpdateable, IMouseMotionListener{
	/** the rendering color*/
	protected float [] color;
	/**
	 * 
	 * @param size the screen size
	 */
	public AbstractButton(final Vector2f size) {
		this.size = size;
	}
	
	public void setSize(final Vector2f size){
		this.size = size;
	}
	
	public abstract void onClick();
	public abstract void onPress();
	
	public void update(){
		if(isMouseInside()){
			onMouseOver();	
			if(MouseManager.getLeftButtonState() == MouseState.CLICKED){
				onClick();
				// XXX: create a real event or something.
				notifyListeners("Clicked");				
			}else if(MouseManager.getLeftButtonState() == MouseState.PRESSED){
				onPress();
			}
			
		}else{
			onMouseOut();
		}
	}
	
	@Override
	public final boolean isMouseInside() {
		final Vector2f wc = parent.getWorldCoordinates(localLocation);
		return CalculationUtils.isPointInside(wc, size, MouseManager.getMouseCoords());
	}


}
